<?php
/**
 * 账号管理
 * User: raisound
 * Date: 2021/10/13
 */
namespace app\api\controller;

use app\common\business\CommonBus as CommonBus;
use app\api\business\UserInfoBus as UserInfoBus;
use app\common\model\redis\RedisModel;
use think\facade\Db;

class UserInfo extends AuthBase
{
    /**
     * 列表
     * @return \josn
     */
    public function list(){
        try{
            $data = array();
            // 数据接收
            $data['page'] = input("param.page",0,"intval"); // 当前页码
            $data['limit'] = input("param.limit",0,"intval"); // 限定条数
            $bus = new UserInfoBus();
            $result = $bus->listData($data);
            if($result){
                return returnResult(config("config.code.success"),config("config.describe.success"),$result);
            }else{
                return returnResult(config("config.code.success"),config("config.describe.null_data"),$result);
            }
        }catch (\Exception $e){
            exception($e,'UserInfo.list');
        }
    }

    /**
     * 新增信息
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function add()
    {
        try{
            $data = array();
            // 数据接收
            $data['name'] = input("param.name",'',"trim");
            $data['user_number'] = input("param.user_number",'',"trim");
            $data['user_pass'] = input("param.user_pass",'',"trim");
            // 业务处理
            $bus = new CommonBus('UserInfo');
            $result = $bus->getFieldInfo('user_number',$data['user_number'],true);
            if($result){
                return returnResult(config("config.code.error"),'账户已存在！');
            }
            $data['user_pass'] = md5($data['user_pass']);
            $result = $bus->addData($data);
            if($result){
                return returnResult(config("config.code.success"),config("config.describe.success"),$data);
            }else{
                return returnResult(config("config.code.success"),config("config.describe.null_data"),[]);
            }
        }catch (\Exception $e){
            exception($e,'UserInfo.add');
        }
    }

    /**
     * 查看信息
     */
    public function see(){
        try{
            $id = input("param.id",'',"trim");
            if(empty($id)){
                return returnResult(config("config.code.error"),'用户id错误',[]);
            }
            $commBus = new CommonBus('UserInfo');
            $res = $commBus->getFieldInfo('id',$id,true);
            if(empty($res)){
                return returnResult(config("config.code.error"),'用户id错误','');
            }
            if($res){
                $res['user_pass'] = '';
                return returnResult(config("config.code.success"),config("config.describe.success"),$res);
            }else{
                return returnResult(config("config.code.error"),config("config.describe.update_failed"),[]);
            }
        }catch (\Exception $e){
            exception($e,'UserInfo.see');
        }
    }

    /**
     * 修改信息
     * @return \josn
     */
    public function modify(){
        try{
            $data = array();
            // 数据接收
            $data['id'] = input("param.id",'',"trim");
            $data['name'] = input("param.name",'',"trim");
            $data['user_number'] = input("param.user_number",'',"trim");
            $data['user_pass'] = input("param.user_pass",'',"trim");
            // 账户检查
            $commBus = new CommonBus('UserInfo');
            $info = $commBus->getFieldInfo('id',$data['id'],true);
            if(empty($info)){
                return returnResult(config("config.code.error"),'ID不存在！','');
            }
            // 判断密码是否有修改
            if($info['user_pass']){
                $data['user_pass'] = md5($data['user_pass']);
            }
            $result = $commBus->updateData($data);
            if($result){
                return returnResult(config("config.code.success"),config("config.describe.success"),$result);
            }else{
                return returnResult(config("config.code.error"),config("config.describe.update_failed"),[]);
            }
        }catch (\Exception $e){
            exception($e,'UserInfo.modify');
        }
    }

    /**
     * 删除信息
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete()
    {
        try{
            $id = input('param.id','',"intval");
            if(!is_numeric($id) || $id==0 ){
                return returnResult(config("config.code.error"),config("config.describe.error_id"));
            }
            // 业务处理
            $bus = new CommonBus('UserInfo');
            $res = $bus->getFieldInfo('id',$id,true);
            if(empty($res)){ 
                return returnResult(config("config.code.error"),config("config.describe.error_id"));
            }
            $result = $bus->deleteData($id);
            if($result){
                return returnResult(config("config.code.success"),config("config.describe.success"),[]);
            }else{
                return returnResult(config("config.code.success"),config("config.describe.deletion_failed"),[]);
            }
        }catch (\Exception $e){
            exception($e,'UserInfo.delete');
        }
    }

    /**
     * 更新token时间
     * @return \josn
     */
    public function heartbeat(){
        try{
            // 业务处理
            $result = \app\api\business\UserInfoBus::heartbeat($this->xToken);
            if($result){
                return returnResult(config("config.code.success"),config("config.describe.success"),[]);
            }else{
                return returnResult(config("config.code.not_login"),config("config.describe.not_login"),[]);
            }
        }catch (\Exception $e){
            exception($e,'UserInfo.heartbeat');
        }
    }

    /**
     * 通过token获取用户信息
     * @return \josn
     */
    public function tokenInfo()
    {
        try{
            // 参数接收
            $RedisModel = new RedisModel();
            $userInfo  = $RedisModel->getValue(config("redis.token_pre").$this->xToken);
            $userInfo = json_decode($userInfo,true);
            if(empty($userInfo)){
                return returnResult(config("config.code.not_login"),config("config.describe.not_login"),[]);
            }
            // 业务处理
            $result = \app\api\business\UserInfoBus::getUserByUserId($userInfo['id']);
            if($result){
                $q = Db::table('menu')->select();
                $list = [];
                foreach ($q as $index =>$value) {
                    if($value['levels']==0){
                        if(empty($value['url'])){
                            $value['url'] = '/';
                        }
                        $q=[
                            'path'=>"/",
                            'component'=>'Layout',
                            'redirect'=>'/'.$value['url'],
                            'children'=>[[
                                'path'=>$value['url'],
                                'component'=>$value['url'],
                                'name'=>$value['url'],
                                'meta'=>['title'=>$value['title'], 'icon'=>'size']
                            ]]
                        ];
                        $list[$value['id']]=$q;
                    }else{
                        $s = [
                            'path'=>"/",
                            'component'=>'Layout',
                            'name'=>$value['url'],
                            'meta'=>['title'=>$value['title'], 'icon'=>'size']
                        ];
                        array_push($list[$value['levels']]['children'],$s);
                    }
                }
                $result['roles'] = array_merge($list);
                return returnResult(config("config.code.success"),config("config.describe.success"),$result);
            }else{
                return returnResult(config("config.code.success"),config("config.describe.null_data"),[]);
            }
        }catch (\Exception $e){
            exception($e,'UserInfo.tokenInfo');
        }
    }

    public function logout(){

    }

}